package com.lhn.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author lhn
 * @date 2024-08-22 11:07
 */

@Component
@Aspect
public class ProjectAdvice {

    @Pointcut("execution(* com.lhn.service.*Service.*(..))")
    private void pointCut_service(){}

    @Around("pointCut_service()")
    public Object runSpeed(ProceedingJoinPoint pjp) throws Throwable {
        //获取一个签名对象。通过传递进来的pjp对象（通常是一个代表连接点（JoinPoint）
        // 的对象，用于获取方法执行时的信息，常见于AOP编程中)
        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();

        Long startTime = System.currentTimeMillis();
        Object o = null;
        for (int i = 0; i < 10000; i++) {
            o = pjp.proceed();
        }
        Long endTime = System.currentTimeMillis();
        System.out.println(className + " 的 " + methodName + " 方法，运行1w次速度为：" + (endTime - startTime) + "ms");
        return o;
    }
}
